﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace PlcLib.Model
{
    public class Net
    {
        /// <summary>
        /// 读写最大超时毫秒,默认3000毫秒
        /// </summary>
        public ushort MaxMilliseconds { get; set; } = 3000;

        /// <summary>
        /// 下位机IP地址 格式 192.168.0.1
        /// </summary>
        public string IP { get; set; } = "127.0.0.1";
        /// <summary>
        /// 下位机网络端口
        /// </summary>
        public int Port { get; set; } = 502;
        public IPAddress IPAddress
        {
            get
            {
                try
                {
                    return IPAddress.Parse(IP);
                }
                catch
                {
                    throw new Exception($"{IP}不能转换成IPAddress对象");
                }
            }
        }
        /// <summary>
        /// PLC的IP最后一位
        /// </summary>
        public byte ServerLastNode => IPAddress.GetAddressBytes().Last();
        /// <summary>
        /// 电脑的IP最后一位
        /// </summary>
        public byte ClientLastNode => LocalIPAddress.GetAddressBytes().Last();


        /// <summary>
        /// 上位机IP地址 格式 192.168.0.1
        /// </summary>
        public string LocalIP { get; set; } = "127.0.0.1";
        /// <summary>
        /// 上位机网络端口
        /// </summary>
        public int LocalPort { get; set; } = 502;
        public IPAddress LocalIPAddress
        {
            get
            {
                try
                {
                    return IPAddress.Parse(LocalIP);
                }
                catch
                {
                    throw new Exception($"{LocalIP}不能转换成IPAddress对象");
                }
            }
        }
    }
}
